package com.customview.wzq.zrwidgetdemo;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import java.io.File;

/**
 * 创建时间：2017/7/5
 * 作者：kb_jay
 * 功能描述：
 */

public class ListWidgeService extends RemoteViewsService {

    public static final String KEY_MODE = "mode";
    public static final String MODE_IMAGE = "image";
    public static final String MODE_MEDIA = "media";

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new ListRemoteViewFactory(this, intent);
    }

    private class ListRemoteViewFactory implements RemoteViewsFactory {

        private final int mAppWidgetId;
        Context mContext;
        private Cursor mDataCursor;

        public ListRemoteViewFactory(Context context, Intent intent) {
            mContext = context;
            mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        @Override
        public void onCreate() {
            //获得一个widget对应的SharedPerferences文件
            SharedPreferences sp = mContext.getSharedPreferences(String.valueOf(mAppWidgetId), Context.MODE_PRIVATE);
            //获得该widget的Mode类型
            String mode = sp.getString(KEY_MODE, MODE_IMAGE);
            //如果mode是视频类型，则去query系统的视频数据表获得对应的cursor
            if (MODE_MEDIA.equals(mode)) {
                String[] projection = {
                        MediaStore.Video.Media.TITLE,
                        MediaStore.Video.Media.DATE_TAKEN,
                        MediaStore.Video.Media.DATA
                };
                mDataCursor = MediaStore.Images.Media.query(getContentResolver(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection);
            } else {
                //如果mode是图片类型，则去query系统的图片数据表获得对应的cursor
                String[] projection = {
                        MediaStore.Images.Media.TITLE,
                        MediaStore.Images.Media.DATE_TAKEN,
                        MediaStore.Images.Media.DATA
                };
                mDataCursor = MediaStore.Images.Media.query(getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection);
            }

        }

        @Override
        public void onDataSetChanged() {
            mDataCursor.requery();
        }

        @Override
        public void onDestroy() {
            mDataCursor.close();
            mDataCursor = null;
        }

        @Override
        public int getCount() {
            return mDataCursor.getCount();
        }

        @Override
        public RemoteViews getViewAt(int position) {
            mDataCursor.moveToPosition(position);
            //widget中的ListView中的Item也要包装为RemoteView
            //该代码与BaseAdapter的getView中获取Item布局的代码类似
            RemoteViews rv=new RemoteViews(getPackageName(),R.layout.widget_list_item);
            rv.setTextViewText(R.id.line1, mDataCursor.getString(0));
            //DateFormat是一个安卓提供的工具类，可以省去用SimpleDateFormat去parse的步骤了
            rv.setTextViewText(R.id.line2, DateFormat.format("MM/dd/yyyy", mDataCursor.getLong(1)));
            SharedPreferences sp=mContext.getSharedPreferences(String.valueOf(mAppWidgetId), MODE_PRIVATE);
            String mode=sp.getString(KEY_MODE, MODE_IMAGE);
            String type;
            if(MODE_MEDIA.equals(mode)){
                type="video/*";
            }else{
                type="image/*";
            }
            Uri data=Uri.fromFile(new File(mDataCursor.getString(2)));
            Intent intent=new Intent();
            intent.setDataAndType(data, type);
            //setOnClickFillInIntent与 setPendingIntentTemplate可以联合使用
            //当在widgets中使用集合（比如说ListView, StackView等等），为一个个单独的Item设置PendingIntents是非常麻烦的，
            //通过设置PendingIntentsTemplate可以一次性给集合里面的Item设置相同的点击时动作，
            //如果希望某一个Item点击后的动作有所不同，就为这个Item单独使用fillInIntent来设置它点击后执行的Intent。
            rv.setOnClickFillInIntent(R.id.ll_widget_item, intent);
            return rv;
        }

        @Override
        public RemoteViews getLoadingView() {
            return null;
        }

        @Override
        public int getViewTypeCount() {
            return 1;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }
    }
}
